package game.UIComponent {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import game.common.CustomBitmap;
	import game.common.Reflection;
	import game.utils.ColorConstant;
	import game.utils.Utils;
	
	public class Button extends UIBase
	{
		private var _height:Number = 0
		protected var upStyle:String;
		protected var overStyle:String;
		protected var downStyle:String;
		protected var disabledStyle:String;
		protected var backGround:CustomBitmap = new CustomBitmap();
		private var word:TextField= new TextField();
		protected var baseY:int;
		protected var _enabled:Boolean = true;
		public var overFn:Function;
		public var outFn:Function;
		private var _width:Number = 0;
		
		/**
		 *  
		 * @param text 按钮上的文字
		 * @param style 按钮的背景
		 */		
		public function Button(text:String, style:String)
		{
			buttonMode=true;
			
			upStyle = style + "_up";
			overStyle = style + "_over";
			downStyle = style + "_down";
			
//			if(style == 'button1' || style == 'button' || style == 'add' || style == 'suo')
//				disabledStyle =style + '_disabled'
			
			backGround.source = upStyle;
			addChild(backGround);
			
			/**若要显示文字,则按钮周围会被透明textfield遮住,导致鼠标离开按钮之后也不会变回正常模式*/
//			word = Utils.getTextField(text, 12, ColorConstant.getColorInt(32));
//			word.x = (backGround.width - word.width)/2;
//			word.y = (backGround.height - word.height)/2;
//			addChild(word);
			
			addEventListener(MouseEvent.ROLL_OVER, rollover);
			addEventListener(MouseEvent.ROLL_OUT, rollout);
//			addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
//			addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		public function set style(value:String):void
		{
			upStyle = value + "_up";
			overStyle = value + "_over";
			downStyle = value + "_down";
			
			backGround.source = upStyle;
		}
		
//		public function set label(value:String):void {
//			word.text = value;
//			if(_width != 0) word.x = (_width - word.width)/2;
//			else word.x = (backGround.width - word.width)/2;
//			
//			if(_height != 0) word.y = (_height - word.height)/2;
//			else word.y = (backGround.height - word.height)/2;
//		}
//		public function get label():String {
//			return word.text;
//		}
		
		public override function set height(value:Number):void {
			backGround.scaleY = value / backGround.height;
			word.y = (value - word.textHeight)/2 + 10;
			_height = value;
		}
		
		
		public override function set width(value:Number):void {
			backGround.scaleX = value / backGround.width;
			word.x = (value - word.width)/2;
			_width = value;
		}
		
		public function get enabled():Boolean {
			return _enabled;
		}
		
		override public function set enabled(value:Boolean):void {
			this.mouseChildren = this.mouseEnabled = _enabled = value;
			//backGround.source = _enabled ? upStyle : disabledStyle;
			if(enabled) 
				filters = [];
			else 
				filters = Global.enabled;
		}
		
		
		protected function rollover(e:MouseEvent):void {
			backGround.source = overStyle;
		//	word.color = ColorConstant.getColorInt(31);
			if(overFn != null) overFn.call(null,e);
		}
		
		protected function rollout(e:MouseEvent):void {
			backGround.source = upStyle;
		//	word.color = ColorConstant.getColorInt(32);
//			if(!_enabled)
//				backGround.source = disabledStyle;
//			if(outFn != null) 
//				outFn.call(null,e);
		}
		
//		protected function mousedown(e:MouseEvent):void {
//			backGround.source = downStyle;
//		}
		
//		protected function mouseUp(e:MouseEvent):void {
//			backGround.source = upStyle;
//		}
		
		public function dispose():  void {
			removeChild(backGround);
			removeChild(word);
			super.removeEventListener(MouseEvent.ROLL_OVER, rollover);
			super.removeEventListener(MouseEvent.ROLL_OUT, rollout);
			//super.removeEventListener(MouseEvent.MOUSE_DOWN, mousedown);
			//super.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
	}
}
